from urlparse import urlunparse
from twisted.web import http

def _parse(url, defaultPort=None):
    url = url.strip()
    parsed = http.urlparse(url)
    scheme = parsed[0]
    path = urlunparse(('', '') + parsed[2:])

    if defaultPort is None:
        if scheme == 'https':
            defaultPort = 443
        else:
            defaultPort = 80

    host, port = parsed[1], defaultPort
    if ':' in host:
        host, port = host.split(':')
        try:
            port = int(port)
        except ValueError:
            port = defaultPort

    if path == '':
        path = '/'

    return scheme, host, port, path

class SiftJob(object):

    def __init__(self, url, priority=1):
        self.setURL(url)
        self.priority = priority

    def setURL(self, url):
            self.url = url
            scheme, host, port, path = _parse(url)
            if scheme and host:
                self.scheme = scheme
                self.host = host
                self.port = port
            self.path = path

    
